/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.test.dqsy.quiry.web;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.test.dqsy.quiry.entity.FfLeftTrees;
import com.jeeplus.modules.test.dqsy.quiry.service.FfLeftTreesService;

/**
 * fdsController
 * @author 于金洋
 * @version 2020-10-04
 */
@RestController
@RequestMapping(value = "/dqsy/quiry/ffLeftTrees")
public class FfLeftTreesController extends BaseController {

	@Autowired
	private FfLeftTreesService ffLeftTreesService;

	@ModelAttribute
	public FfLeftTrees get(@RequestParam(required=false) String id) {
		FfLeftTrees entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = ffLeftTreesService.get(id);
		}
		if (entity == null){
			entity = new FfLeftTrees();
		}
		return entity;
	}


	/**
	 * fsda树表数据
	 */
	@RequiresPermissions("dqsy:quiry:ffLeftTrees:list")
	@GetMapping("list")
	public AjaxJson list(FfLeftTrees ffLeftTrees) {
		return AjaxJson.success().put("list", ffLeftTreesService.findList(ffLeftTrees));
	}

	/**
	 * 根据Id获取fsda数据
	 */
	@RequiresPermissions(value={"dqsy:quiry:ffLeftTrees:view","dqsy:quiry:ffLeftTrees:add","dqsy:quiry:ffLeftTrees:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(FfLeftTrees ffLeftTrees) {
		return AjaxJson.success().put("ffLeftTrees", ffLeftTrees);
	}

	/**
	 * 保存fsda
	 */
	@RequiresPermissions(value={"dqsy:quiry:ffLeftTrees:add","dqsy:quiry:ffLeftTrees:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(FfLeftTrees ffLeftTrees, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(ffLeftTrees);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		ffLeftTreesService.save(ffLeftTrees);//保存
		return AjaxJson.success("保存fsda成功");
	}

	/**
	 * 删除fsda
	 */
	@RequiresPermissions("dqsy:quiry:ffLeftTrees:del")
	@DeleteMapping("delete")
	public AjaxJson delete(FfLeftTrees ffLeftTrees) {
		ffLeftTreesService.delete(ffLeftTrees);
		return AjaxJson.success("删除fsda成功");
	}

	/**
	     * 获取JSON树形数据。
	     * @param extId 排除的ID
	     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<FfLeftTrees> list = ffLeftTreesService.findList(new FfLeftTrees());
		List rootTree = getFfLeftTreesTree(list, extId);
		return AjaxJson.success().put("treeData", rootTree);
	}

	private List<FfLeftTrees> getFfLeftTreesTree(List<FfLeftTrees> list, String extId) {
		List<FfLeftTrees> ffLeftTreess = Lists.newArrayList();
		List<FfLeftTrees> rootTrees = ffLeftTreesService.getChildren("0");
		for (FfLeftTrees root : rootTrees) {
		    if (StringUtils.isBlank(extId) ||  !extId.equals(root.getId())) {
		        ffLeftTreess.add(getChildOfTree(root, list, extId));
		    }
		}
		return ffLeftTreess;
	}

	private FfLeftTrees getChildOfTree(FfLeftTrees ffLeftTrees, List<FfLeftTrees> ffLeftTreesList, String extId) {
		ffLeftTrees.setChildren(Lists.newArrayList());
		for (FfLeftTrees child : ffLeftTreesList) {
		    if (StringUtils.isBlank(extId) ||  (!extId.equals(child.getId()) && child.getParentIds().indexOf("," + extId + ",") == -1)) {
		        if (child.getParentId().equals(ffLeftTrees.getId())) {
		            ffLeftTrees.getChildren().add(getChildOfTree(child, ffLeftTreesList, extId));
		        }
		    }
		}
		return ffLeftTrees;
	}



}